package com.zg.job.config;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import org.quartz.spi.JobFactory;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SpringBeanJobFactory;

import java.util.Properties;

/**
 * @author Zxb
 */
@Configuration
public class QuartzConfig {

//   @Autowired
//   private ApplicationContext applicationContext;
//
//
//
//    @Bean
//    public SchedulerFactoryBean schedulerFactoryBean() {
//        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
//        schedulerFactoryBean.setQuartzProperties(quartzProperties());
//        schedulerFactoryBean.setJobFactory(new SpringBeanJobFactory(applicationContext));
//        schedulerFactoryBean.setOverwriteExistingJobs(true);
//        schedulerFactoryBean.setAutoStartup(true); // 确保仅启动一次
//        return schedulerFactoryBean;
//    }
//
//    @Bean
//    public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean) throws SchedulerException {
//        return schedulerFactoryBean.getScheduler();
//    }
//
//    private Properties quartzProperties() {
//        Properties properties = new Properties();
//        // 配置线程池相关属性
//        properties.put("org.quartz.threadPool.class", "org.quartz.simpl.SimpleThreadPool");
//        // 线程池大小，根据实际需求调整
//        properties.put("org.quartz.threadPool.threadCount", "10");
//        // 线程优先级
//        properties.put("org.quartz.threadPool.threadPriority", "5");
//        // 线程是否为守护线程
//        properties.put("org.quartz.threadPool.makeThreadsDaemons", "true");
////        properties.setProperty("org.quartz.jobStore.class", "org.quartz.impl.jdbcjobstore.JobStoreTX");
////        properties.setProperty("org.quartz.jobStore.driverDelegateClass", "org.quartz.impl.jdbcjobstore.StdJDBCDelegate");
////        properties.setProperty("org.quartz.jobStore.tablePrefix", "QRTZ_");
////        properties.setProperty("org.quartz.jobStore.isClustered", "true");
//        return properties;
//    }
//
//    public static class SpringBeanJobFactory extends AdaptableJobFactory {
//        private final AutowireCapableBeanFactory beanFactory;
//
//        public SpringBeanJobFactory(ApplicationContext context) {
//            this.beanFactory = context.getAutowireCapableBeanFactory();
//        }
//
//        @Override
//        protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
//            Object job = super.createJobInstance(bundle);
//            // 关键：将Job实例交给Spring管理
//            beanFactory.autowireBean(job);
//            return job;
//        }
//    }

}
